package com.xywl.escort.view.certified;

import com.xywl.escort.bean.AddUserAutoInfoResp;
import com.xywl.escort.bean.AddUserAutoReq;
import com.xywl.escort.bean.UploadImageResp;
import com.xywl.escort.net.BaseSubscriber;
import com.xywl.escort.net.ExceptionHandle;
import com.xywl.escort.net.api.service.ApiServer;
import com.xywl.escort.utils.RxUtil;

import org.reactivestreams.Publisher;

import java.io.File;

import javax.inject.Inject;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Function;
import io.reactivex.functions.Predicate;
import io.reactivex.schedulers.Schedulers;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

/**
 * Created by 32422 on 2017/10/25.
 */

public class CertifiedPresenter implements CertifiedContract.Presenter {
    private CertifiedContract.View mView;
    private ApiServer mApiServer;
    private String identityObverse = "";
    private String identityReverse = "";

    @Inject
    CertifiedPresenter(ApiServer apiServer) {
        mApiServer = apiServer;
    }

    @Override
    public void subscribe() {

    }

    @Override
    public void unSubscribe() {
        mView = null;
    }

    @Override
    public void takeView(CertifiedContract.View view) {
        this.mView = view;
    }

    @Override
    public void uploadImage(final String positive, final String negative, final String userId, final String id) {
        final File file = new File(positive);
        if (file.exists()) {
            RequestBody requestBody = RequestBody.create(MediaType.parse("multipart/form-data"), file);
            MultipartBody.Part part = MultipartBody.Part.createFormData("file", file.getName(), requestBody);
            mApiServer.uploadImage(part)
                    .compose(RxUtil.<UploadImageResp>applySchedulers(RxUtil.IO_ON_UI_TRANSFORMER_BACK_PRESSURE))
                    .compose(mView.<UploadImageResp>bindToLife())
                    .filter(new Predicate<UploadImageResp>() {
                        @Override
                        public boolean test(UploadImageResp resp) {
                            return null != resp && null != resp.data && resp.isOk();
                        }
                    })
                    .observeOn(Schedulers.io())
                    .map(new Function<UploadImageResp, MultipartBody.Part>() {
                        @Override
                        public MultipartBody.Part apply(UploadImageResp resp) {
                            identityObverse = resp.data.url;
                            File file1 = new File(negative);
                            if (file1.exists()) {
                                RequestBody requestBody1 = RequestBody.create(MediaType.parse("multipart/form-data"), file1);
                                return MultipartBody.Part.createFormData("file", file1.getName(), requestBody1);
                            }
                            return null;
                        }
                    })
                    .flatMap(new Function<MultipartBody.Part, Publisher<UploadImageResp>>() {
                        @Override
                        public Publisher<UploadImageResp> apply(MultipartBody.Part req) {
                            return mApiServer.uploadImage(req);
                        }
                    })
                    .observeOn(AndroidSchedulers.mainThread())
                    .filter(new Predicate<UploadImageResp>() {
                        @Override
                        public boolean test(UploadImageResp resp) {
                            return null != resp && null != resp.data && resp.isOk();
                        }
                    })
                    .observeOn(Schedulers.io())
                    .map(new Function<UploadImageResp, AddUserAutoReq>() {
                        @Override
                        public AddUserAutoReq apply(UploadImageResp resp) {
                            identityReverse = resp.data.url;
                            AddUserAutoReq req = new AddUserAutoReq();
                            req.identityObverse = identityObverse;
                            req.identityReverse = identityReverse;
                            req.userId = userId;
                            req.id = id;
                            return req;
                        }
                    })
                    .flatMap(new Function<AddUserAutoReq, Publisher<AddUserAutoInfoResp>>() {
                        @Override
                        public Publisher<AddUserAutoInfoResp> apply(AddUserAutoReq req) {
                            return mApiServer.addUserAutoInfo(req);
                        }
                    })
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new BaseSubscriber<AddUserAutoInfoResp>() {
                        @Override
                        protected void hideDialog() {

                        }

                        @Override
                        protected void showDialog() {

                        }

                        @Override
                        public void onError(ExceptionHandle.ResponseThrowable e) {
                            mView.uploadImageFailure("操作失败");
                        }

                        @Override
                        public void onNext(AddUserAutoInfoResp resp) {
                            if (null != resp) {
                                if (resp.isOk()) {
                                    mView.uploadImageSuccess(resp);
                                } else {
                                    mView.uploadImageFailure(resp.msg);
                                }
                            } else {
                                mView.uploadImageFailure("操作失败");
                            }
                        }
                    });
        }
    }
}
